package com.sddnytsh.body_buildingsystem.utils

import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import java.util.concurrent.ConcurrentHashMap

/**
 * 头像缓存管理器 - 预生成Bitmap头像，避免矢量测量
 */
object AvatarCache {
    private val cache = ConcurrentHashMap<String, Bitmap>()
    private val colors = listOf(
        Color.parseColor("#FF6B6B"), // 红色
        Color.parseColor("#4ECDC4"), // 青色
        Color.parseColor("#45B7D1"), // 蓝色
        Color.parseColor("#96CEB4"), // 绿色
        Color.parseColor("#FFEAA7"), // 黄色
        Color.parseColor("#DDA0DD"), // 紫色
        Color.parseColor("#98D8C8"), // 薄荷绿
        Color.parseColor("#F7DC6F"), // 金色
        Color.parseColor("#BB8FCE"), // 淡紫
        Color.parseColor("#85C1E9")  // 天蓝
    )
    
    /**
     * 初始化头像缓存
     */
    fun initialize() {
        if (cache.isNotEmpty()) return
        
        // 生成字母头像
        for (ch in 'A'..'Z') {
            val bitmap = createAvatarBitmap(ch.toString(), getColorForChar(ch))
            cache[ch.toString()] = bitmap
        }
        
        // 生成数字头像
        for (i in 0..9) {
            val bitmap = createAvatarBitmap(i.toString(), getColorForChar(i.toString().first()))
            cache[i.toString()] = bitmap
        }
        
        // 生成常见中文字符头像
        val commonChars = listOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "0")
        for (char in commonChars) {
            if (!cache.containsKey(char)) {
                val bitmap = createAvatarBitmap(char, getColorForChar(char.first()))
                cache[char] = bitmap
            }
        }
    }
    
    /**
     * 创建头像Bitmap
     */
    private fun createAvatarBitmap(text: String, backgroundColor: Int): Bitmap {
        val size = 96 // 高分辨率，支持不同DPI
        val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        
        // 绘制圆形背景
        val paint = Paint().apply {
            isAntiAlias = true
            color = backgroundColor
        }
        canvas.drawCircle(size / 2f, size / 2f, size / 2f, paint)
        
        // 绘制文字
        val textPaint = Paint().apply {
            isAntiAlias = true
            color = Color.WHITE
            textSize = size * 0.4f
            textAlign = Paint.Align.CENTER
            typeface = Typeface.DEFAULT_BOLD
        }
        
        val textBounds = Rect()
        textPaint.getTextBounds(text, 0, text.length, textBounds)
        val y = size / 2f + textBounds.height() / 2f
        
        canvas.drawText(text, size / 2f, y, textPaint)
        
        return bitmap
    }
    
    /**
     * 根据字符获取颜色
     */
    private fun getColorForChar(char: Char): Int {
        val index = char.code % colors.size
        return colors[index]
    }
    
    /**
     * 获取头像Bitmap
     */
    fun getAvatarBitmap(author: String): Bitmap? {
        if (cache.isEmpty()) {
            initialize()
        }
        
        val firstChar = author.firstOrNull()?.toString() ?: "?"
        return cache[firstChar] ?: run {
            // 如果缓存中没有，动态生成
            val bitmap = createAvatarBitmap(firstChar, getColorForChar(firstChar.first()))
            cache[firstChar] = bitmap
            bitmap
        }
    }
    
    /**
     * 清理缓存
     */
    fun clearCache() {
        cache.values.forEach { it.recycle() }
        cache.clear()
    }
}

/**
 * Compose头像组件 - 使用预生成Bitmap
 */
@Composable
fun CachedAvatar(
    author: String,
    size: Dp = 48.dp,
    contentDescription: String? = null
): ImageBitmap? {
    val context = LocalContext.current
    
    return remember(author) {
        AvatarCache.getAvatarBitmap(author)?.asImageBitmap()
    }
}
